package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author zm
 * @since 2022-06-30
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private IApArticleContentService apArticleContentService;
    @Override
    public ResponseResult<Long> saveApArticle(ArticleDto dto) {

        if (dto.getId()==null) {  //新增
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);
            apArticle.setCreatedTime(new Date());
            //新增文章信息
            this.save(apArticle);
            //新增文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);

            return ResponseResult.okResult(apArticle.getId());
        }else{   //修改
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);
            apArticle.setCreatedTime(new Date());
            //修改文章信息
            this.updateById(apArticle);
            //修改文章内容
            LambdaUpdateWrapper<ApArticleContent> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(ApArticleContent::getContent,dto.getContent());
            updateWrapper.eq(ApArticleContent::getArticleId,dto.getId());
            apArticleContentService.update(updateWrapper);
            return ResponseResult.okResult(apArticle.getId());
        }

    }

    @Override
    public ResponseResult load(ArticleHomeDto dto, int type) {  //0 代表初次加载或加载更新 1代表加载更多
        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件：频道id 如果是0 就是查询所有频道
        if (dto.getChannelId()!=0) {
            queryWrapper.eq(ApArticle::getChannelId,dto.getChannelId());
        }

        if(type==0){
            //0 代表初次加载或加载更新  发布时间>maxTime
            queryWrapper.gt(ApArticle::getPublishTime,dto.getMaxTime());
        }else{
            //1代表加载更多  发布时间<minTime
            queryWrapper.lt(ApArticle::getPublishTime,dto.getMinTime());
        }
        //隐藏了两个条件： 是否下架 是否删除
        queryWrapper.eq(ApArticle::getIsDelete,false);
        queryWrapper.eq(ApArticle::getIsDown,false);

        queryWrapper.orderByDesc(ApArticle::getPublishTime)  ;


        Page<ApArticle> page = new Page<>(1,dto.getSize());

        Page<ApArticle> page1 = this.page(page, queryWrapper);

        return ResponseResult.okResult(page1.getRecords());
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {

        //根据文章id查询ap_article表
        ApArticle apArticle = this.getById(dto.getArticleId());
        if(apArticle==null){
            log.error("查不到数据，文章id是："+dto.getArticleId());
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //根据文章id查询 ap_article_content 表
        LambdaQueryWrapper<ApArticleContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleContent::getArticleId,dto.getArticleId());
        ApArticleContent apArticleContent = apArticleContentService.getOne(queryWrapper);

        Map resultMap = new HashMap();
        resultMap.put("config",apArticle);
        resultMap.put("content",apArticleContent);

        return ResponseResult.okResult(resultMap);
    }
}
